package com.example.poemtest.index.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.StudyPoetryBean;
import com.example.poemtest.find.view.ExpandTextView;

import java.util.List;

/**
 * 右侧每一个itme的适配器
 * created by JHT on 2024/3/25
 */
public class PoetryItemAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<StudyPoetryBean.StudyPoetry> poetryDatas;
    private InnerItemOnclickListener mListener;
    private String poetryFenlei;
//    ViewHold viewHold;

    public PoetryItemAdapter(Context context, List<StudyPoetryBean.StudyPoetry> poetryDatas) {
        this.context = context;
        this.poetryDatas = poetryDatas;
    }


    @Override
    public int getCount() {
        if (poetryDatas != null) {
            return poetryDatas.size();
        } else {
            return 10;
        }
    }

    @Override
    public Object getItem(int position) {
        return poetryDatas.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold;
        StudyPoetryBean.StudyPoetry subcategory = poetryDatas.get(position);
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_study_poetry, null);
            viewHold = new ViewHold();
            viewHold.tv_title = (TextView) convertView.findViewById(R.id.tv_fenge);
            viewHold.poetryTitle = (TextView) convertView.findViewById(R.id.poetry_title);
            viewHold.poetryAuther = (TextView) convertView.findViewById(R.id.poetry_auther);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        //设置分割线
        //如果一样就隐藏
        if (position == 0) {
            Log.e("正确",position+"");
            viewHold.tv_title.setVisibility(View.VISIBLE);
            poetryFenlei = subcategory.getPoetryTypeDetial();
            viewHold.tv_title.setText("------------" + poetryFenlei + "------------");
        } else {
            if (subcategory.getPoetryTypeDetial().equals(poetryFenlei)) {
                viewHold.tv_title.setVisibility(View.GONE);
            } else {
                //如果不一样就设置
                viewHold.tv_title.setVisibility(View.VISIBLE);
                viewHold.tv_title.setText("------------" + subcategory.getPoetryTypeDetial() + "------------");
                poetryFenlei = subcategory.getPoetryAuthorDynasty();
            }
        }
        viewHold.poetryTitle.setText(subcategory.getPoetryName());
        viewHold.poetryAuther.setText(subcategory.getPoetryAuthor());
//        if (subcategory.getId().equals("主食")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList1[position]);
//        } else if (subcategory.getId().equals("炸串类")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList2[position]);
//        } else if (subcategory.getId().equals("肉类")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList3[position]);
//        } else if (subcategory.getId().equals("海鲜类")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList4[position]);
//        } else if (subcategory.getId().equals("蛋制品")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList5[position]);
//        } else if (subcategory.getId().equals("水果")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList6[position]);
//        } else if (subcategory.getId().equals("零食")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList7[position]);
//        } else if (subcategory.getId().equals("冷饮")) {
//            viewHold.foodIcon.setImageResource(Constant.imgList8[position]);
//        }

        return convertView;
    }

    public interface InnerItemOnclickListener {
        void itemClick(View v);
    }

    public void setOnInnerItemOnClickListener(InnerItemOnclickListener listener){
        this.mListener=listener;
    }

    @Override
    public void onClick(View v) {
        setOnInnerItemOnClickListener(mListener);
        mListener.itemClick(v);
    }


    /**
     * 内部类
     */
    private class ViewHold {
        private TextView poetryTitle, poetryAuther;
        private TextView tv_title;
    }
}
